package com.asiainfo.kbisheet.util

import groovy.util.XmlSlurper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.asiainfo.kbisheet.vo.DBTypeVO;
import com.asiainfo.kbisheet.vo.DataSourceVO;
import com.asiainfo.kbisheet.vo.SettingVO;

class Setting {
	public static final String SETTING_FILE = "setting.xml";
	protected static final Log log = LogFactory.getLog(Setting.class)
	static SettingVO settingVO = new SettingVO()
	static{
		def settingXML = new XmlSlurper().parse(Thread.currentThread().getContextClassLoader().getResource(SETTING_FILE).getFile())
		log.info "初始化系统配置..."
		String subsource = settingXML.@subsource
		log.info "子配置文件 = " + subsource
		/*得到运行模式*/
		settingVO.mode = settingXML.@mode
		log.info "运行模式 = " + settingVO.mode
		
		/*得到运行模式*/
		settingVO.debug = settingXML.@debug == 'true'
		log.info "显示debug内容 = " + settingVO.debug
		
		settingVO.debug_xml = settingXML.@debug_xml == 'true'
		log.info "显示XML内容 = " + settingVO.debug_xml
		
		
		/*得到数字类型*/
		settingVO.numbers = settingXML.datatype.number.text().split(",")
		log.info "数字类型 = " + settingVO.numbers
		
		/*得到日期类型*/
		settingVO.dates = settingXML.datatype.date.text().split(",")
		log.info "日期类型 = " + settingVO.dates
		
		/*数据类型*/
		settingVO.dbtypes = new ArrayList<DBTypeVO>()		
		settingXML.dbtypes.dbtype.each { dbtypeXML->
			DBTypeVO dbTypeVO = new DBTypeVO(typeName:dbtypeXML.@typeName,driverClassName:dbtypeXML.driverClassName,dbuClass:dbtypeXML.dbuClass)
			settingVO.dbtypes.add(dbTypeVO)
			log.info ">> 数据库驱动 = " + dbTypeVO.driverClassName
		}

		/*读取SubSetting*/
		def subsettingXML = new XmlSlurper().parse(Thread.currentThread().getContextClassLoader().getResource(subsource).getFile())
		
		settingVO.subkey = subsettingXML.@subkey
		settingVO.buClass = subsettingXML.@buClass
		log.info "BU实现类 = " + settingVO.buClass
		
		settingVO.kbsControlImpl = subsettingXML.@kbsControlImpl
		log.info "kbsControl实现类 = " + settingVO.kbsControlImpl
		
		/*得到模板文件的根目录*/
		settingVO.filesRoot = subsettingXML.files.root.find{ it.@mode.text().indexOf(settingVO.mode) > -1 || it.@mode.text() == ''}
		log.info "模板文件的根目录 = " + settingVO.filesRoot
		/*存放KBiSheet数据的数据源*/
		settingVO.kbsbase = subsettingXML.datasources.@kbsbase
		log.info "存放KBiSheet数据的数据源 = " + settingVO.kbsbase
		/*数据源*/
		settingVO.datasources = new ArrayList<DataSourceVO>()		
		subsettingXML.datasources.datasource.each { datasourceXML->
			if(datasourceXML.@mode.text() == '' || datasourceXML.@mode.text().indexOf(settingVO.mode) > -1){				
				DataSourceVO dataSourceVO = new DataSourceVO(typeName:datasourceXML.@typeName,key:datasourceXML.@key,url:datasourceXML.url,user:datasourceXML.user,password:datasourceXML.password,usepool:(datasourceXML.@usepool == "true"))
				dataSourceVO.properties = new Properties()
				datasourceXML.properties.property.each { propertyXML->
					dataSourceVO.properties.setProperty(propertyXML.@key.text(), propertyXML.@value.text())
				}
				settingVO.datasources.add(dataSourceVO)
				log.info ">> 数据源 = " + dataSourceVO.url + "; usepool = " + dataSourceVO.usepool + "; properties = " + dataSourceVO.properties.size() + dataSourceVO.properties
			}
		}

		/*是否允许全部导出*/
		settingVO.enableAllExport = subsettingXML.allexport.@enable == "true"
		
		/*得到维表列*/
		settingVO.dimColumns = subsettingXML.dimColumns.text().split(",")
		log.info "维表列 = " + settingVO.dimColumns
			
		log.info "初始化系统配置完成!"
	}
	
	public static String getWebRoot(){
		(new File((new File((new File(Thread.currentThread().getContextClassLoader().getResource(SETTING_FILE).getPath())).getParent())).getParent())).getParent()
	}
}
